﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace Painter.Model
{
    public class ScaleData: INotifyPropertyChanged
    {
        double mScale = 1, mBaseWidth, mBaseHeight;
        public event PropertyChangedEventHandler PropertyChanged;
        static Dictionary<ScaleData, List<UIElement>> mDataMap=new Dictionary<ScaleData, List<UIElement>>();

        public static ScaleData Find_CreateScaleData(UIElement ui)
        {
            KeyValuePair<ScaleData, List<UIElement>> keyValue = 
                mDataMap.Where(o => o.Value.Contains(ui) ||
                    o.Value.Contains((ui as FrameworkElement)?.Parent) ||
                    o.Value.Contains((ui as Decorator)?.Child)||
                    o.Value.Contains((ui as ContentControl)?.Content)).FirstOrDefault();
            if (keyValue.Value == null)
            {
                keyValue = new KeyValuePair<ScaleData, List<UIElement>>(new ScaleData(), new List<UIElement>());
                mDataMap.Add(keyValue.Key, keyValue.Value);
            }
            if (!keyValue.Value.Contains(ui))
            {
                keyValue.Value.Add(ui);
            }
            return keyValue.Key;
        }

        public double Scale
        {
            get { return mScale; }
            set { mScale = value; }
        }
        public double BaseWidth
        {
            get { return mBaseWidth; }
            set { mBaseWidth = value; }
        }
        public double BaseHeight
        {
            get { return mBaseHeight; }
            set { mBaseHeight = value; }
        }
        public void UpdateWidthHeight()
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Sz_Width_Height"));
        }
        public string Sz_Width_Height
        {
            get { return (int)BaseWidth + "x" + (int)BaseHeight + "像素"; }
        }
        public double ZoomInOut(bool In)
        {
            mScale = In ? (mScale > 1 ? mScale - 1 : mScale / 2.0) : (mScale > 1 ? mScale + 1 : mScale * 2.0);
            mScale = (mScale <= 0.125 ? 0.125 : mScale);
            mScale = (mScale >= 10 ? 10 : mScale);
            return mScale;
        }
    }
}
